#include "include.h"

#if PWM_RGB_EN

#define PWM_RGB_PR                  255*64

//LPWM0, LPWM2, LPW3对应R, G, B控制
void pwm_init(void)
{
    GPIOEDE |= (BIT(4) | BIT(6) | BIT(7));
    GPIOEDIR &= ~(BIT(4) | BIT(6) | BIT(7));
    GPIOE &= ~(BIT(4) | BIT(6) | BIT(7));
    FUNCMCON1 = LPWM0MAP_PE4 | LPWM2MAP_PE6 | LPWM3MAP_PE7;

    PWMPRCON = PWM_RGB_PR;
    PWMCON = 0;                     //初始化disable pwm
}

void pwm_rgb_write(u8 red, u8 green, u8 blue)
{
    PWM01DUTY = ((u32)red << 6);
    PWM23DUTY = ((u32)green << 6) | ((u32)blue << (6 + 16));
    if ((PWMCON & 0x0D) != 0x0D) {
        FUNCMCON1 = LPWM0MAP_PE4 | LPWM2MAP_PE6 | LPWM3MAP_PE7;
        PWMCON |= 0x0D;             //enbale LPWM0, LPWM2, LPWM3(R,G,B)
    }
}

void pwm_rgb_close(void)
{
    FUNCMCON1 = 0xff0f0000;
    PWMCON = 0;
}


/*
void pwm_test(void)
{
    printf("%s\n", __func__);
    GPIOADE |= 0x0f;
    GPIOADIR &= ~0x0f;              // PA0 ~ PA3 对应 lowpower PWM0~PWM3 Mapping G3
    GPIOA &= ~0x0f;
    FUNCMCON1 = (3 << 16) | (3 << 20) | (3 << 24) | (3 << 28);

    PWMPRCON = 0x6590;              //PWM period = (PWMPR+1)，分辨率26MHz/4    0x6590->4ms
    PWM01DUTY = 0x32c84c2c;         //DUTY: PWMIVN=0:设置高电平时间； PWMIVN=1:设置低电平时间
    PWM23DUTY = 0x19641964;

    PWMSTEP = 0x68686868;           //PWMSTEP有效位7bit，最高位为符号位 （按STEP加减调节到DUTY或0） 0x68->16us
    PWMCYCNUM = 0x01010101;         //CYCNUM当前STEP的PWM输出次数 = PWMCYCNUM + 1

    PWMCON = 0x1f;
}
*/
#endif // PWM_RGB_EN


#if ENERGY_LED_EN    //能量灯软件PWM输出.

#define  ENERGY_LED_NUM 4         //灯的个数
#define  ENERGY_PWM_MAX_DUTY  8   //一个灯分8个亮度.

#define  ENERGY_LED_INIT()     {GPIOBDE |= 0x0F; GPIOBDIR &= ~0x0F;}
#define  ENERGY_LED_0_ON()     GPIOBSET = BIT(0)
#define  ENERGY_LED_0_OFF()    GPIOBCLR = BIT(0)
#define  ENERGY_LED_1_ON()     GPIOBSET = BIT(1)
#define  ENERGY_LED_1_OFF()    GPIOBCLR = BIT(2)
#define  ENERGY_LED_2_ON()     GPIOBSET = BIT(2)
#define  ENERGY_LED_2_OFF()    GPIOBCLR = BIT(2)
#define  ENERGY_LED_3_ON()     GPIOASET = BIT(3)
#define  ENERGY_LED_3_OFF()    GPIOACLR = BIT(3)

u8 pwm_duty_buf[ENERGY_LED_NUM];

AT(.com_text.rgb.tbl)   //非线性量化表.
u8 const energy_qtz_tbl[4 * 8] = {
     1,14,18,22,26,30,35,40,
     41,43,46,48,51,53,56,60,
     61,63,66,68,71,73,76,80,
     81,83,85,88,91,93,96,100,
};

void energy_led_init(void)
{
    ENERGY_LED_INIT();
}

AT(.com_text.rgb)
void energy_led_level_calc(void)    //约5ms调用一次.
{
    u8 level,i;
    u16 energy;
    static u8 disp_level,last_level;
    static u8 time_5ms_cnt = 0;

    time_5ms_cnt++;
    if(time_5ms_cnt >= 4){   //20ms计算一次能量
        time_5ms_cnt  = 0;

        energy = dac_pcm_pow_calc();
        //printf("[%X_%d]",rgb_led_en,energy);
        if (FUNC_MUSIC == func_cb.sta) {   //各模式能量效果可能不一样.
            energy = energy/450;
        } else if (FUNC_FMRX == func_cb.sta) {
            energy = energy/350;
        } else{
            energy = energy/300;
        }
        //非线性量化表
        for (i = 0, last_level = 0; i < 4*8; i++) {
           if (energy < energy_qtz_tbl[i]) {
               break;
           }
           last_level++;
        }
    }
    //能量相同, 不用更新
    if(disp_level == last_level){
        return;
    }

    //能量减少时,慢慢下降
    if (disp_level > last_level) {
        disp_level--;
    } else {
        disp_level = last_level;
    }

    //能量转为占空比,存放到pwm_duty_buf中.
    level = disp_level;
    for (i=0; i<ENERGY_LED_NUM; i++){
        if (level >= ENERGY_PWM_MAX_DUTY) {
            pwm_duty_buf[i] = 0;
            level -= ENERGY_PWM_MAX_DUTY;
        } else {
            pwm_duty_buf[i] = (1<<level) - 1;
            pwm_duty_buf[i] = ~pwm_duty_buf[i];
            level = 0;
        }
    }
}

AT(.com_text.rgb)
void energy_led_scan(void)  //建议1ms扫描一次.
{
    static u8 scan_bit = 0;
    if (pwm_duty_buf[0] & BIT(scan_bit)){
        ENERGY_LED_0_OFF();
    } else {
        ENERGY_LED_0_ON();
    }

    if (pwm_duty_buf[1] & BIT(scan_bit)) {
        ENERGY_LED_1_OFF();
    } else {
        ENERGY_LED_1_ON();
    }

    if (pwm_duty_buf[2] & BIT(scan_bit)) {
        ENERGY_LED_2_OFF();
    } else {
        ENERGY_LED_2_ON();
    }

    if (pwm_duty_buf[3] & BIT(scan_bit)) {
        ENERGY_LED_3_OFF();
    } else {
        ENERGY_LED_3_ON();
    }

    if (++scan_bit >= ENERGY_PWM_MAX_DUTY){ //ENERGY_PWM_MAX_DUTY= 8
        scan_bit = 0;
    }
}
#endif // ENERGY_LED_EN

#if RGB_SERIAL_EN
#define MAX_RGB_NUM 8
#define MAX_COLOR_INDEX 3 //RGBW
#if 0
const u8 rgb_color_table[5][3] =
{
	{0x6b,		0x8b,		0x00,	},		//teal 蓝色
	{0x00,		0x70,		0xE0,	},		//pink 粉色
	{0x20,		0x80,		0x80,	},		//purple 紫色
	{0x00,		0x00,		0xB0,	},		//red 红色
	{0x00,		0xFF,		0x00,	},		//blue 蓝色
};
#endif
// G->R->B->W
AT(.com_text.rgb.tbl)
const u8 all_colors_table[16][MAX_COLOR_INDEX]=
{
	{215,	255,    0,	},		//yellow
	{130,	231,    169,},		//pink
	{22,	175,    	133,},		//magenta
	{94,	160,    	181,},		//light purple
	{38,	155,    	182,},		//purple
	{194,	108,    74,	},		//green
	{203,	89,	    232,},		//light blue
	{149,	0,		  200,},		//blue
	{0,		0,		  0	,},		//off
	{0,		176,    0,},		//red 
	{128,	255,	0,},		//orange
	{200,	255,	0,},		//orange little
	{200,	200,    200,},		//red
	{0,		255,		  0	,},		//off
	{215,	0,    0	,},		//red
	{0,	0,    232	,},		//red
};
/**************************************************************************************************
///由于时序要求严格，建议使用120M系统时钟，否则因为系统时间更改导致SPI 波特率变化引起时序错误
***************************************************************************************************/
#define RGB_BAUD    600000  //每1bit的时间是300ns，4bit的时间刚好是1.25us左右，与灯驱动的时间差不多
#define RGB_POWER_CTL_PIN 5 //PA5
#define RGB_1_PIN   4		//PA4
#define PIN_1_GROUP 1
#define PIN_2_GROUP 2
#define MAX_RGB_SIZE MAX_RGB_NUM*MAX_COLOR_INDEX*4 //MAX_RGB_NUM*4*4 //6组RGB$（6*4）,那一个RGB 需要4byte存储(每4bit生成一个byte的RGB数据)，
#define MAX_REST_SIZE 100 //复位码为低电平 >=80us ,1byte 2.5us 
#define BUF_OFFSET 12+33
u8 rgb_buf[MAX_RGB_SIZE+MAX_REST_SIZE];   //rgb数据BUF

AT(.com_text.rgb)
void com_memcpy(void *dst,const void *src,size_t length)
{
	#if 1
	memcpy(dst,src,length);
	#else
	while(length--)
	{
		*(char*)dst=*(char*)src;
		dst=(char*)dst+1;
		src=(char*)src+1;
	}
	#endif
}
AT(.com_text.rgb)
void com_memset(void *s,int c,size_t n)
{
	#if 1
	memset(s,c,n);
	#else
	const unsigned char uc = c;
	unsigned char *su;
	for(su = s;0 < n;++su,--n)
	*su = uc;
	#endif
}

AT(.com_text.rgb)
void rbg_data_change(u8* src_data,u8* dst_data)
{
	u8 i,tmp_length=MAX_RGB_NUM*MAX_COLOR_INDEX,val;
	u16 tmp_data;
	u16 j=0;
	u8* tmp_dst=dst_data;
	while(tmp_length)
	{
		tmp_data=(*src_data)/2;
		//tmp_dst=dst_data+k*BUF_OFFSET;
		i=8;
		while(i)
		{
			val=(tmp_data>>(i-1))&0x01;
			if(val==1)
			{
				if(i%2==0)
				{
					tmp_dst[j]|=0xE0;
				}
				else
				{
					tmp_dst[j]|=0x0E;
				}
			}
			else
			{
				if(i%2==0)
				{
					tmp_dst[j]|=0x80;
				}
				else
				{
					tmp_dst[j]|=0x08;
				}

			}
			i--;
			if(i%2==0)
			{
				j++;
			}

		}
		src_data++;
		tmp_length--;
		//k++;
	}
}
#if 0
AT(.com_text.rgb)
void rgb_data_switch(u8* data,u8 length)
{
	u8 tmp_data,i;
	for(i=0;i<length/2;i++)
	{
		tmp_data=data[i];
		data[i]=data[length-i-1];
		data[length-i-1]=tmp_data;
	}
}
#endif
AT(.com_text.str)
char printf_str[]="spi read!!\n";

AT(.com_text.rgb)
void int_rgb_pin(u8 pin_num,u8 group)
{
	#if 0
	while(spi_get_read_status())
	{
		WDT_CLR();
		printf(printf_str);
		delay_5ms(2);		
	}
	#endif
	GPIOAFEN |= BIT(pin_num);
    GPIOADE |=  BIT(pin_num);
    GPIOADIR &= ~(BIT(pin_num));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (group<<12);  //G1
	SPI1BAUD = 24000000/RGB_BAUD - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
	SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
}
#if 0
//AT(.com_text.rgb)
void rgb_light_group1_display()
{
	GPIOAFEN |= BIT(4);
    GPIOADE |=  BIT(4);
    GPIOADIR &= ~(BIT(4));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (0x01<<12);  //G1
	//SPI1CON =  0x00;
	SPI1BAUD = 24000000/RGB_BAUD - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
	//print_r(rgb_buf,36);
	SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
}
//AT(.com_text.rgb)
void rgb_light_group2_display(void)
{
	GPIOAFEN |= BIT(7);
    GPIOADE |=  BIT(7);
    GPIOADIR &= ~(BIT(7));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (0x02<<12);  //G2
	//SPI1CON =  0x00;
	SPI1BAUD = 24000000/RGB_BAUD - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
	SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
}
#endif

AT(.com_text.rgb)
void rgb_light_set(u8* color_table)
{
	u8 tmp_color_table[MAX_RGB_NUM*MAX_COLOR_INDEX],i,tmp_table[MAX_RGB_NUM];
	
	com_memset(rgb_buf,0x00,sizeof(rgb_buf));
	com_memset(tmp_color_table,0x00,MAX_RGB_NUM*MAX_COLOR_INDEX);
	com_memcpy(tmp_table,color_table,MAX_RGB_NUM);
	for(i=0;i<MAX_RGB_NUM;i++)
	{
		//printf("color talbe:%d\n",tmp_table[i]);
		com_memcpy(&tmp_color_table[i*MAX_COLOR_INDEX],&all_colors_table[tmp_table[i]][0],MAX_COLOR_INDEX);
		#if 0
		if(i<3)
		{
			com_memcpy(&tmp_color_table[i*3],&all_colors_table[tmp_table[2-i]][0],3);
		}
		else
		{
			com_memcpy(&tmp_color_table[i*3],&all_colors_table[tmp_table[8-i]][0],3);
		}
		#endif
	}
	
	rbg_data_change(tmp_color_table,rgb_buf);
	int_rgb_pin(RGB_1_PIN,PIN_1_GROUP);
	delay_ms(1);
	//delay_us(300);	
	#if 1
	//spiflash1_init();
	//delay_ms(1);
	#else
	if(func_cb.sta==FUNC_EXSPIFLASH_MUSIC||func_cb.sta==FUNC_IDLE||func_cb.sta==FUNC_EXSPIFLASH_DEMO)
	{
		spiflash1_init();
		delay_us(300);
	}
	else if(func_cb.sta==FUNC_BT)
	{
		SPI1CON=0;
	}
	#endif
}
u8 get_fade_diff(u8 current_color, u8 to_color, u16 fade_counter, u16 fade_total)
{
	u16 fade_diff;
	//current_color=(current_color*light_info.rgb_time)/255;
	//to_color=(to_color*light_info.rgb_time)/255;
	if(current_color > to_color)
	{
		fade_diff = current_color - to_color;
		fade_diff = (u8)((u16)(fade_diff * fade_counter) / fade_total);
		fade_diff = current_color - fade_diff;
	}
	else
	{
		fade_diff = to_color - current_color;
		fade_diff = (u8)((u16)(fade_diff * fade_counter )/ fade_total);
		fade_diff = current_color + fade_diff;

	}

	return (uchar)fade_diff;
}
/* 颜色渐变模式 glow 模式 = (0~255) */
u8 display_glow_mode(disp_status_t* p_disp_status)
{
	u8 i = 0,j=0;
	u8 rgb_tmp[24];	/* fade 差值 */
	u8 cur_color_table[24],next_color_table[24];
	//printf("fade_steps:%d->switch_time:%d\n",p_disp_status->fade_steps,p_disp_status->color_switch_time);
	if(p_disp_status->fade_steps > p_disp_status->color_switch_time)
	{
		p_disp_status->fade_steps = 0;
		return 1;
	}
	com_memset(rgb_buf,0x00,sizeof(rgb_buf));
	com_memset(rgb_tmp,0x00,24);
	com_memset(cur_color_table,0x00,24);
	for(i=0;i<6;i++)
	{
		//memcpy(&cur_color_table[i*3],&all_colors_table[p_disp_status->cur_color_table[5-i]][0],3);
		//memcpy(&next_color_table[i*3],&all_colors_table[p_disp_status->next_color_table[5-i]][0],3);
		if(i<3)
		{
			memcpy(&cur_color_table[i*3],&all_colors_table[p_disp_status->cur_color_table[2-i]][0],3);
			memcpy(&next_color_table[i*3],&all_colors_table[p_disp_status->next_color_table[2-i]][0],3);
		}
		else
		{
			memcpy(&cur_color_table[i*3],&all_colors_table[p_disp_status->cur_color_table[8-i]][0],3);
			memcpy(&next_color_table[i*3],&all_colors_table[p_disp_status->next_color_table[8-i]][0],3);
		}
	}
	//printf("fade_steps:%d\n",p_disp_status->fade_steps);
	for(j=0;j<8;j++)
	{
		for(i = 0; i < 3; i++)
		{
			rgb_tmp[i+j*3] = get_fade_diff(
				cur_color_table[i+j*3],
				next_color_table[i+j*3],
				p_disp_status->fade_steps,
				p_disp_status->color_switch_time);
		}
		//print_r(rgb_tmp,3);
	}
	rbg_data_change(rgb_tmp,rgb_buf);
	int_rgb_pin(RGB_1_PIN,PIN_1_GROUP);
	delay_ms(1);
	#if 1
	//spiflash1_init();
	//delay_ms(1);
	#else
	if(func_cb.sta==FUNC_EXSPIFLASH_MUSIC||func_cb.sta==FUNC_IDLE||func_cb.sta==FUNC_EXSPIFLASH_DEMO)
	{
		spiflash1_init();
		delay_us(300);
	}
	else if(func_cb.sta==FUNC_BT)
	{
		SPI1CON=0;
	}	
	#endif
	return 0;
}

void rgb_power_onoff(u8 onoff)
{
	if(onoff)
	{
		GPIOADE |= BIT(RGB_POWER_CTL_PIN);
		GPIOADIR &= ~BIT(RGB_POWER_CTL_PIN);
		GPIOASET = BIT(RGB_POWER_CTL_PIN);
	}
	else
	{
		GPIOADE |= BIT(RGB_POWER_CTL_PIN);
		GPIOADIR &= ~BIT(RGB_POWER_CTL_PIN);
		GPIOACLR = BIT(RGB_POWER_CTL_PIN);
	}
}

#if 0
void rgb_led1_display(void)
{
	GPIOAFEN |= BIT(4);
    GPIOADE |=  BIT(4);
    GPIOADIR &= ~(BIT(4));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (0x01<<12);  //G1
	SPI1CON =  0x00;
	SPI1BAUD = 24000000/RGB_BAUD - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
    memset(rgb_buf,0x00,sizeof(rgb_buf));
	rbg_data_change(&rgb_color_table[2][0],rgb_buf);
	//print_r(rgb_buf,36);
	SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
}
void rgb_led1_reset(void)
{
	u8 reset_buf[9];
	GPIOAFEN |= BIT(4);
    GPIOADE |=  BIT(4);
    GPIOADIR &= ~(BIT(4));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (0x01<<12);  //G1
	SPI1CON =  0x00;
	SPI1BAUD = 24000000/RGB_BAUD - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
    memset(rgb_buf,0x00,sizeof(rgb_buf));
	memset(reset_buf,0x00,9);
	rbg_data_change(reset_buf,rgb_buf);
	printf("BBBBBB\n");
	//print_r(rgb_buf,36);
	SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
}

void rgb_led2_display(void)
{
	GPIOAFEN |= BIT(7);
    GPIOADE |=  BIT(7);
    GPIOADIR &= ~(BIT(7));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (0x02<<12);  //G2
	SPI1CON =  0x00;
	SPI1BAUD = 24000000/RGB_BAUD - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
    memset(rgb_buf,0x00,sizeof(rgb_buf));
	rbg_data_change(&rgb_color_table[0][0],rgb_buf);
	//print_r(rgb_buf,36);
	SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
}
void rgb_led2_reset(void)
{
	u8 reset_buf[9];
	GPIOAFEN |= BIT(4);
    GPIOADE |=  BIT(4);
    GPIOADIR &= ~(BIT(4));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (0x02<<12);  //G1
	SPI1CON =  0x00;
	SPI1BAUD = 24000000/RGB_BAUD - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
    memset(rgb_buf,0x00,sizeof(rgb_buf));
	memset(reset_buf,0x00,9);
	rbg_data_change(reset_buf,rgb_buf);
	//print_r(rgb_buf,36);
	SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
}

void rgb_spi1_init(u32 baud)
{
    //SPI1_G1: CLK_PA3, DIO_PA4,   //CS_PA5
//    GPIOAFEN |= BIT(3)|BIT(4);
//    GPIOADE |=  BIT(3)|BIT(4);
//    GPIOADIR &= ~(BIT(3) | BIT(4));  //CLK,DO output
//    GPIOADIR |= BIT(4);  //DI input
//    GPIOAPU |= BIT(4);   //DI pull up 10K
//    FUNCMCON1 = (0x0F<<12);
//    FUNCMCON1 = (0x01<<12);  //G1


    GPIOAFEN |= BIT(4);
    GPIOADE |=  BIT(4);
    GPIOADIR &= ~(BIT(4));  //CLK,DO output
    FUNCMCON1 = (0x0F<<12);
    FUNCMCON1 = (0x01<<12);  //G1

//    //SPI1_G2:  DIO_PA7,  CLK_PA6, //CS_PA5
//    GPIOAFEN |= BIT(6)|BIT(7);
//    GPIOADE |= BIT(6)|BIT(7);
//    GPIOADIR &= ~ BIT(6) ;  //CLK output
//    GPIOADIR |= BIT(7);  //DI input
//    GPIOAPU |= BIT(7);   //DI pull up 10K
//    FUNCMCON1 = (0x0F<<12);
//    FUNCMCON1 = (0x02<<12); //G2

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    SPI1BAUD = 24000000/baud - 1;   //sysclk/baud - 1
    SPI1CON =  0x01 | (1<<2);
    memset(rgb_buf,0x00,sizeof(rgb_buf));
	rbg_data_change(&rgb_color_table[2][0],rgb_buf);
	printf("rgb data:");
	print_r(rgb_buf,36);
}
/*直接调用此函数更新数据即可*/
AT(.com_text.rgb)
void spi1_senddata(void)
{
    SPI1DMAADR = DMA_ADR(rgb_buf);
    SPI1DMACNT = sizeof(rgb_buf);
//    while(!(SPI1CON & BIT(16))){  //wait send finish
//        WDT_CLR();
//    }
}
void init_io_test(void)
{
    GPIOADE |=  BIT(4);
	GPIOADIR &= ~(BIT(4));  //CLK,DO output
	GPIOACLR=BIT(4);

}
void set_io_onoff(u8 onoff)
{
	GPIOADE |=  BIT(4);
	GPIOADIR &= ~(BIT(4));  //CLK,DO output
	if(onoff)
	{
		GPIOASET=BIT(4);
	}
	else
	{
		GPIOACLR=BIT(4);
	}
}
void delay_time(u16 time)
{
	u16 i,j;
	for(i=0;i<100;i++)
	{
		for(j=0;j<time;j++);
	}
}
void rgb_spi_test(void)
{
	//rgb_led1_display();
	//rgb_led2_display();
    //rgb_spi1_init(RGB_BAUD);
	//spi1_senddata();
//    while(1)
//    {
//        WDT_CLR();
//        spi1_senddata();
//        delay_5ms(100);
//        printf("->");
//    }
}
#endif
#endif // RGB_SERIAL_EN



#if 0  //SYSCLK_OUT_TEST
#define PLL0DIV         SFR_RW (SFR3_BASE + 0x23*4)
#define PLL1DIV         SFR_RW (SFR3_BASE + 0x24*4)
const char strp[] = "\nPWRCON0 = 0x%X,PLL0CON = 0x%X, PLL0DIV=0x%X\n";
const char strp1[] = "CLKCON0_1= 0x%X,0X%X\n";
const char strp2[] = "CLKCON2_3= 0x%X,0X%X\n";
void print_sysclk_info(void)
{
    static u32 ticks = 0;
    if (tick_check_expire(ticks,1000)) {
        ticks = tick_get();
        printf(strp,PWRCON0,PLL0CON,PLL0DIV);
        printf(strp1,CLKCON0,CLKCON1);
        printf(strp2,CLKCON2,CLKCON3);
    }
}

u8 test_buf[360];   //rgb数据BUF
void test_sysclk_out(void)  //default PA3输出，显示需要选择为GUI_NO
{
    static bool init = false;
    if (!init) {
        init = true;
        //SPI1_G1: CLK_PA3
        GPIOAFEN |= BIT(3);
        GPIOADE |=  BIT(3);
        GPIOADIR &= ~BIT(3);    //CLK output
        FUNCMCON1 = (0x0F<<12);
        FUNCMCON1 = (0x01<<12);  //G1

        SPI1BAUD = 9;   //sysclk/baud - 1
        SPI1CON =  0x01 | (1<<2);
        SPI1DMAADR = DMA_ADR(test_buf);
        SPI1DMACNT = sizeof(test_buf);
    }
    WDT_CLR();
    print_sysclk_info();
    if (SPI1CON & BIT(16)) {
        SPI1DMAADR = DMA_ADR(test_buf);
        SPI1DMACNT = sizeof(test_buf);
    }
}
#endif    //SYSCLK_OUT_TEST



